# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=openal
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.21.1
pkgrel=3
pkgdesc="OpenAL audio library for use with opengl (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/kcat/openal-soft"
license=('GPL2')
depends=("${MINGW_PACKAGE_PREFIX}-libmysofa")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-fluidsynth"
             "${MINGW_PACKAGE_PREFIX}-portaudio")
options=('strip' 'staticlibs')
source=(https://github.com/kcat/openal-soft/archive/${pkgver}.tar.gz
        0001-versioned-w32-dll.mingw.patch
        0002-w32ize-portaudio-loading.mingw.patch
        0003-openal-not-32.mingw.patch
        0004-disable-OSS-windows.patch
        0005-mingw-dont-check-libm.patch
        0006-gcc-11-add-missing-include.patch)
sha256sums=('8ac17e4e3b32c1af3d5508acfffb838640669b4274606b7892aa796ca9d7467f'
            'c3e56b5594ada0b95588373bd8ca062677805beae393c6551301518ab24a0cd3'
            'f0bde7d3a8087530ab1bb2f48e59eec6f4cf60d59c5afe3a0c28c95e6b138751'
            '33dccfc603a48d265341e60bc801976d077e546ab5c91a93e8426eac01bb7ad4'
            '8b8ff007d224a5267aead184dc6d24317e40be69c91064cdfdc35b78bb762cc6'
            'c2528ecc034ea993cb63893b3f2ce5ceeac8d63295decf6864f58cd646c74299'
            'b5cab28da6d8ffa4b994ed49720ef4b066a663800d0f412c1e571f48ece6dc35')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd "${srcdir}/openal-soft-${pkgver}"
  apply_patch_with_msg \
    0001-versioned-w32-dll.mingw.patch \
    0002-w32ize-portaudio-loading.mingw.patch \
    0003-openal-not-32.mingw.patch \
    0004-disable-OSS-windows.patch \
    0005-mingw-dont-check-libm.patch \
    0006-gcc-11-add-missing-include.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  [[ -d "${srcdir}"/shared-${MSYSTEM} ]] && rm -rf ${srcdir}/shared-${MSYSTEM}
  mkdir -p "${srcdir}/shared-${MSYSTEM}" && cd "${srcdir}/shared-${MSYSTEM}"

  DXSDK_DIR=${MINGW_PREFIX}/${MINGW_CHOST} \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${extra_config[@]}" \
    -DALSOFT_EXAMPLES=OFF \
    -DALSOFT_UTILS=ON \
    -DALSOFT_NO_CONFIG_UTIL=ON \
    -DLIBTYPE=SHARED \
    -DDSOUND_INCLUDE_DIR=${MINGW_PREFIX}/${MINGW_CHOST}/include \
    ../openal-soft-${pkgver}
  ${MINGW_PREFIX}/bin/cmake.exe --build ./

  [[ -d "${srcdir}"/static-${MSYSTEM} ]] && rm -rf ${srcdir}/static-${MSYSTEM}
  mkdir -p "${srcdir}/static-${MSYSTEM}" && cd "${srcdir}/static-${MSYSTEM}"

  # enabling utilities on static causes an undefined reference error.
  DXSDK_DIR=${MINGW_PREFIX}/${MINGW_CHOST} \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${extra_config[@]}" \
    -DALSOFT_EXAMPLES=OFF \
    -DALSOFT_UTILS=OFF \
    -DALSOFT_NO_CONFIG_UTIL=ON \
    -DLIBTYPE=STATIC \
    -DDSOUND_INCLUDE_DIR=${MINGW_PREFIX}/${MINGW_CHOST}/include \
    ../openal-soft-${pkgver}
  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd "${srcdir}/static-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install

  cd "${srcdir}/shared-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install
}
